import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";

Vue.use(VueRouter);
import store from "../store/index";

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
    beforeEnter(to, from, next) {
      console.log("路由规则独享守卫");
      next();
    },
  },
  {
    path: "/about",
    name: "about",
    component: () => import("../views/AboutView.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

// router.beforeEach((to, from, next) => {
//   console.log("全局前置守卫");
//   // if (localStorage.getItem("token")) {
//   if (store.state.token) {
//     next();
//   } else {
//     if (to.path == "/login") {
//       next();
//     } else {
//       next("/login");
//     }
//   }
// });
//全局前置守卫

router.beforeResolve((to, from, next) => {
  console.log("全局解析守卫");
  next();
});
router.afterEach(() => {
  console.log("全局后置守卫");
});

export default router;
